// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   EntityBuilder.java

package tool.radmodel;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;

// Referenced classes of package com.qdone.radmodel:
//			CodeBuilder, Project, Field, ColumnConvert, 
//			Entity

public class EntityBuilder
	implements CodeBuilder
{

	private Entity entity;
	private Project project;

	public EntityBuilder(Project project)
	{
		this.project = project;
		entity = this.project.getEntity();
	}

	private String getDataType(int iDataType)
	{
		//根据字段类型的值，返回java的数据类型
		String dataType = "";
		if (iDataType == 12 || iDataType == 1 || iDataType == -1 || iDataType == 2005)
			dataType = "String";
		else
		if (iDataType == 4 || iDataType == -5 || iDataType == -7 || iDataType == -6 || iDataType == 2)
			dataType = "Integer";
		else
		if (iDataType == 8 || iDataType == 6)
			dataType = "Double";
		else
		if (iDataType == 91 || iDataType == 93 || iDataType == 92)
			dataType = "Date";
		else
		if (iDataType == 2004||iDataType==-4)
			dataType = "byte[]";
		else if(iDataType==3){
			dataType = "BigDecimal";
		}
		else if(iDataType==7){
			dataType = "Float";
		}
		return dataType;
	}

	public String createSetter(Field field)
	{
		StringBuffer codeBuffer = new StringBuffer();
		codeBuffer.append((new StringBuilder("    public void set")).append(ColumnConvert.getGetSetterNameByProp(field.getPropertyName())).append("(").append(getDataType(field.getDataType())).append(" ").append(field.getPropertyName()).append(") {\r\n").toString());
		codeBuffer.append((new StringBuilder("        this.")).append(field.getPropertyName()).append(" = ").append(field.getPropertyName()).append(";\r\n").toString());
		codeBuffer.append("    }\r\n\r\n");
		return codeBuffer.toString();
	}

	public String createGetter(Field field)
	{
		StringBuffer codeBuffer = new StringBuffer();
		codeBuffer.append((new StringBuilder("    public ")).append(getDataType(field.getDataType())).append(" get").append(ColumnConvert.getGetSetterNameByProp(field.getPropertyName())).append("() {\r\n").toString());
		codeBuffer.append((new StringBuilder("        return this.")).append(field.getPropertyName()).append(";\r\n").toString());
		codeBuffer.append("    }\r\n\r\n");
		return codeBuffer.toString();
	}

	public void saveToFile()
	{
		StringBuffer codeBuffer = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		codeBuffer.append((new StringBuilder("package ")).append(project.getEntitiesPackage()).append(";\r\n\r\n").toString());
		codeBuffer.append("import java.util.Date;\r\n\r\n");
		codeBuffer.append("/**\r\n");
		codeBuffer.append("  *TODO 本代码由代码生成工具生成\r\n");
		codeBuffer.append((new StringBuilder("  *@date")).append(sdf.format(new Date())).append("\r\n").toString());
		codeBuffer.append("*/\r\n");
		codeBuffer.append((new StringBuilder("public class ")).append(entity.getEntityName()).append("{\r\n\r\n").toString());
		codeBuffer.append("    // Fields\r\n\r\n");
		if(entity.getPrimaryKeyField()==null){
		}
		codeBuffer.append((new StringBuilder("    private ")).append(getDataType(entity.getPrimaryKeyField().getDataType())).append(" ").append(entity.getPrimaryKeyField().getPropertyName()).append(";\r\n").toString());
		int iFieldCount = entity.getFields().size();
		for (int i = 0; i < iFieldCount; i++)
		{
			Field field = (Field)entity.getFields().get(i);
			System.out.println(field.getDateTypeName()+":"+field.getDataType());
			codeBuffer.append((new StringBuilder("    private ")).append(getDataType(field.getDataType())).append(" ").append(field.getPropertyName()).append(";\r\n").toString());
		}

		codeBuffer.append("    \r\n");
		codeBuffer.append("    // Constructors\r\n\r\n");
		codeBuffer.append("    /** 默认构造函数 */\r\n");
		codeBuffer.append((new StringBuilder("    public ")).append(entity.getEntityName()).append("() {\r\n").toString());
		codeBuffer.append("    }\r\n\r\n");
		codeBuffer.append("    // Property accessors\r\n\r\n");
		codeBuffer.append(createSetter(entity.getPrimaryKeyField()));
		codeBuffer.append(createGetter(entity.getPrimaryKeyField()));
		for (int i = 0; i < iFieldCount; i++)
		{
			Field field = (Field)entity.getFields().get(i);
			codeBuffer.append(createSetter(field));
			codeBuffer.append(createGetter(field));
		}

		codeBuffer.append("}");
		String fileName = (new StringBuilder(String.valueOf(project.getEntitiesPath()))).append(entity.getEntityName()).append(".java").toString();
		File file = new File(fileName);
		if (file.exists())
			file.delete();
		try
		{
			FileUtils.writeStringToFile(file, codeBuffer.toString(), "utf-8");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
